// pages/member/balance_password/balance_password.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    mobile: "",
    start1: false,
    codetext: '获取短信验证码',
    newPwdBol: true,
    newPwdBolConfirm:true,
    newPwd: '',
    newPwdConfirm: '',
    code: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this._getBalancePwdInfo();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  pwdinp(){
    this.setData({
      popupSpecs:true
    });
  },
  //绑定新密码的值
  rePwdinp(e) {
    this.setData({
      popupSpecs2:true
    });
  },
  popupSpecsClose(e){
    this.setData({
      popupSpecs:false
    });
  },
  popupSpecsClose2(e){
    this.setData({
      popupSpecs2:false
    });
  },
  onPassword(e){
    this.setData({
      newPwd:e.detail
    });
  },
  onPassword2(e){
    this.setData({
      newPwdConfirm:e.detail
    });
  },
  //余额支付密码设置 手机号是否绑定
  _getBalancePwdInfo() {
    let that = this;
    let urlStr = app.getNetAddresss("member.balance-password.is-has-password");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (!app._isTextEmpty(res.data.mobile)) {
            that.setData({
              mobile: res.data.mobile
            });
          } else {
            wx.showModal({
              title: '提示',
              content: '先绑定手机号',
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../../member/editmobile/editmobile'
                  });
                } else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1
                  });
                }
              }
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  //绑定验证码的值
  codeinp(e) {
    let val = e.detail.value;
    this.setData({
      "code": val.trim()
    });
  },
  sendCode(e) {
    if (this.data.start1) {
      return false;
    }
    let time = 60;
    let that = this;
    that.setData({
      "start1": true,
      "codetext": "(" + time + ")秒后重新获取"
    });
    let set = setInterval(function() {
      that.setData({
        codetext: "(" + --time + ")秒后重新获取",
      });
    }, 1000);
    setTimeout(function() {
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      clearInterval(set);
    }, 60000);
    that.verificationCode(set);
  },
  //发送验证码
  verificationCode(set) {
    let that = this;
    if (app._isTextEmpty(this.data.mobile) || app._isMoblie(this.data.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '手机号数据错误',
        duration: 1500
      });
      that.setData({
        codetext: "获取短信验证码",
        start1: false
      });
      return;
    }
    let urlStr = app.getNetAddresss("member.balance-password.send-code");
    urlStr += '&mobile=' + this.data.mobile;
    //发送获取验证码的请求
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: '已发送',
            duration: 1500
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  submitInfo() {
    if (app._isTextEmpty(this.data.code)) {
      return app.tips("验证码不能为空");
    }
    if (app._isTextEmpty(this.data.newPwd)) {
      return app.tips("新密码不能为空");
    }
    if (app._isTextEmpty(this.data.newPwdConfirm)) {
      return app.tips("新密码不能为空");
    }
    if (this.data.newPwd!=this.data.newPwdConfirm) {
      return app.tips("密码不一致");
    }



    let that = this;
    let urlStr = app.getNetAddresss("member.balance-password.update-password");
    urlStr += '&password=' + that.data.newPwd;
    urlStr += '&confirmed=' + that.data.newPwdConfirm;
    urlStr += '&code=' + that.data.code;
    urlStr += '&mobile=' + that.data.mobile;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: '修改成功',
            duration: 1500
          });
          setTimeout(function() {
            // 要延时执行的代码
            wx.navigateBack({
              delta: 1
            });
          }, 1500); // 延迟时间
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  newPwdshow() {
    if (this.data.newPwdBol) {
      this.setData({
        newPwdBol: false
      });
    } else {
      this.setData({
        newPwdBol: true
      });
    }
  },
  newPwdConfirmshow() {
    if (this.data.newPwdBolConfirm) {
      this.setData({
        newPwdBolConfirm: false
      });
    } else {
      this.setData({
        newPwdBolConfirm: true
      });
    }
  }
});
